package com.vote.www.utils;

import org.apache.commons.lang.StringUtils;

import java.util.*;

public class MapManage {
	

	public MapManage() {
		super();
	}
	public static boolean isBlank(String string){
		return StringUtils.isBlank(string);
	}
	public static boolean isBlankAnd(String... string){
		for(String str:string){
			if(StringUtils.isNotBlank(str)){
				return false;
			}
		}
		return true;
	}
	public static boolean isBlankOr(String... string){
		for(String str:string){
			if(StringUtils.isBlank(str)){
				return true;
			}
		}
		return false;
	}
	public static boolean isNotBlank(String string){
		return StringUtils.isNotBlank(string);
	}
	public static boolean isNotBlankAnd(String... string){
		for(String str:string){
			if(StringUtils.isBlank(str)){
				return false;
			}
		}
		return true;
	}
	public static boolean isNotBlankOr(String... string){
		for(String str:string){
			if(StringUtils.isNotBlank(str)){
				return true;
			}
		}
		return false;
	}
	/**
	 * 要保留的预留字段(移除不想关的元素)
	 * @param map
	 * @param strings
	 */
	public static void reservedForMap(Map<String, Object> map, String... strings ){
		Set<String> set = map.keySet();
		List<String> list=new ArrayList<String>();
		list.addAll(set);
		for(String string:strings){
			list.remove(string);
		}
		for(String str:list){
			set.remove(str);
		}
	}
	public static Map<String, Object> returnReservedForMap(Map<String, Object> map, String... strings ){
		reservedForMap(map, strings);
		return map;
	}
	/**
	 * 要保留的预留字段(移除不想关的元素)
	 * @param map
	 * @param listStr
	 */
	public static void reservedForMap(Map<String, Object> map, List<String> listStr ){
		Set<String> set = map.keySet();
		List<String> list=new ArrayList<String>();
		list.addAll(set);
		for(String string:listStr){
			list.remove(string);
		}
		for(String str:list){
			set.remove(str);
		}
	}
	public static Map<String, Object> returnReservedForMap(Map<String, Object> map, List<String> listStr){
		reservedForMap(map, listStr);
		return map;
	}
	/**
	 * 要保留的预留字段(移除不想关的元素)
	  * @param map
	 * @param strings
	 */
	public static void reservedForListMap(List<Map<String, Object>> listmap, String... strings ){
		Set<String> set;
		List<String> list;
		if(listmap!=null&&listmap.size()!=0){
			for(int i=0;i<listmap.size();i++){
				for(String st:strings){
					if(listmap.get(i).get(st)==null){
						listmap.get(i).put(st, null);
					}
				}
				set = listmap.get(i).keySet();
				list=new ArrayList<String>();
				list.addAll(set);
				for(String string:strings){
					list.remove(string);
				}
				for(String str:list){
					set.remove(str);
				}
			}
		}
		
	}
	public static List<Map<String, Object>> returnReservedForListMap(List<Map<String, Object>> listmap, String... strings ){
		reservedForListMap(listmap, strings);
		return listmap;
	}
	
	/**
	 *  要保留的预留字段(移除不想关的元素)
	 * @param listmap
	 * @param listStr
	 */
	public static void reservedForListMap(List<Map<String, Object>> listmap, List<String> listStr ){
		Set<String> set;
		List<String> list;
		if(listmap!=null&&listmap.size()!=0){
			for(int i=0;i<listmap.size();i++){
				set = listmap.get(i).keySet();
				list=new ArrayList<String>();
				list.addAll(set);
				for(String string:listStr){
					list.remove(string);
				}
				for(String str:list){
					set.remove(str);
				}
			}
		}
		
	}
	public static List<Map<String, Object>> returnReservedForListMap(List<Map<String, Object>> listmap, List<String> listStr ){
		reservedForListMap(listmap, listStr);
		return listmap;
	}
	/**
	 * list得到MapMap
	 * @param listmap
	 * @param listStr
	 * @return 
	 */
	public static Map<String, Map<String, Object>> toGetKeyMap(String key, List<Map<String, Object>> listMap){
		Map<String, Map<String, Object>> mapobj = new HashMap<String, Map<String, Object>>();
		for(Map<String, Object> li:listMap){
			mapobj.put((String)li.get(key), li);
		}
		return mapobj;
	}

	/**
	 * 替换key
	 * @param map
	 * @param beforeKey 替换前的key
	 * @param afterKey 替换后的key
	 */
	public static void changeMapKey(Map<String, Object> map, String beforeKey, String afterKey) {
		// TODO Auto-generated method stub
		Object value=map.get(beforeKey);
		map.put(afterKey, value);
		map.remove(beforeKey);
	}
	public static void changeListMapKey(List<Map<String, Object>> listMap, String beforeKey, String afterKey) {
		// TODO Auto-generated method stub
		for(int i=0;i<listMap.size();i++){
			Object value=listMap.get(i).get(beforeKey);
			listMap.get(i).put(afterKey, value);
			listMap.get(i).remove(beforeKey);
		}
		
	}
	
	/**
	 * 替换value
	 * @param map
	 * @param key
	 * @param value
	 */
	public static void changeMapValue(Map<String, Object> map, String key, String value) {
		// TODO Auto-generated method stub
		map.remove(key);
		map.put(key, value);
	}

	public static List<String> toGetValueByKey(String key, List<Map<String, Object>> listMap) {
		if (isNotBlank(key)) {
			List<String> listName = new ArrayList<String>();
			for (Map<String, Object> li : listMap) {
				listName.add(String.valueOf(li.get(key)));
			}
			return listName;
		}
		return null;
	}
	/**
	 * 通过listMap得到一个Map《key, List《Map》》
	 * @param key
	 * @param listMap
	 * @return
	 */
	public static Map<String, List<Map<String, Object>>> toGetKeyListMap(String key,
                                                                         List<Map<String, Object>> listMap) {
		if (isNotBlank(key)) {
			List<Map<String, Object>> list = null;
			Map<String, List<Map<String, Object>>> object = new HashMap<String, List<Map<String, Object>>>();
			for (Map<String, Object> li : listMap) {
				if (object.get(li.get(key)) == null) {
					object.put(key, new ArrayList<Map<String, Object>>());
				}
				list = object.get(li.get(key));
				list.add(li);
				object.put(key, list);
			}
			return object;
		}
		return null;
	}
	/**
	 * 通过listMap得到一个Map《key, List《属性value》》
	 * @param key	关联键
	 * @param property	listMap要提取的属性
	 * @param listMap2
	 * @return	返回Map<key, List<属性value>>
	 */
	public static Map<String, List<String>> toGetKeyListString(String key, String property, List<Map<String, Object>> listMap) {
		if (isNotBlankAnd(key,property)) {
			if(listMap!=null&&listMap.size()!=0){
				Map<String, List<String>> mapList = new HashMap<String, List<String>>();
				List<String> listStr = null;
				for (Map<String, Object> map : listMap) {
					if(map.get(key)!=null){
						String value=(String)map.get(key);
							if (mapList.get(value) == null) {
								mapList.put(value, new ArrayList<String>());
							}
							listStr = mapList.get(value);
							listStr.add(String.valueOf(map.get(property)));
							mapList.put(value, listStr);
					}
				}
				return mapList;
			}
		}
		return null;
		
	}
	/**
	 * 通过listMap 把mapliststr的数据加到listmap里，属性用property
	 * @param listMap	
	 * @param mapliststr	key，显示键内容
	 * @param key	关联键
	 * @param property	显示键
	 */
	public static void put(List<Map<String, Object>> listMap, Map<String, List<String>> mapliststr, String key,
                           String property) {
		if (isNotBlankAnd(key, property) && listMap != null && listMap.size() != 0 && mapliststr != null
				&& mapliststr.size() != 0) {
			for (int i = 0; i < listMap.size(); i++) {
				List<String> list = mapliststr.get(listMap.get(i).get(key));
				if(list==null){
					list = new ArrayList<String>();
				}
				listMap.get(i).put(property,list);
			}
		}

	}
	/**
	 * 合并listMap
	 * @param beforeKey	前一个listMap 的关联key
	 * @param afterKey	后一个listMap 的关联key
	 * @param beforeProperty	前一个listMap 的显示属性名
	 * @param afterProperty	后一个listMap 提取 by 属性名
	 * @param beforeListMap	前一个listMap(主体)
	 * @param afterListMap	后一个listMap
	 */
	public static void put(String beforeKey, String afterKey, String beforeProperty, String afterProperty, List<Map<String, Object>> beforeListMap,
                           List<Map<String, Object>> afterListMap) {
		MapManage.put(beforeListMap,MapManage.toGetKeyListString(afterKey,afterProperty, afterListMap),beforeKey,beforeProperty);
	}
	/**
	 * 替换beforeKey里的"xxxx,xxxx,xxxx"并变成一个列表list 
	 * @param listMap 
	 * @param beforeKey			picPaths
	 * @param afterKey			listPicPath
	 */
	public static void changeMapStrsToList(List<Map<String, Object>> listMap, String beforeKey, String afterKey) {
		for(Map<String, Object> map:listMap){
			String value=map.get(beforeKey)==null?"":(String)map.get(beforeKey);
			String[] strs=value.split(",");
			if(isNotBlank(value)){
				List<String> list = Arrays.asList(strs);
				map.put(afterKey, list);
			}else{
				map.put(afterKey, new ArrayList<String>());
			}
			map.remove(beforeKey);
		}
	}

}
